package com.chen.palmar.project.set;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.primb.androidlibs.net.entity.HttpResultEntity;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;

/**
 * Created by chen on 2018/1/8.
 */

public class EditPwdActivity extends BaseActivity {

    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tv_top)
    TextView tvTop;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.et_pwd_old)
    EditText etPwdOld;
    @Bind(R.id.et_pwd_new)
    EditText etPwdNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {

        toolBar.setNavigationOnClickListener(v -> EditPwdActivity.this.finish());

        titleBar.setText("修改密码");

        tvTop.setText("保存");
        tvTop.setVisibility(View.VISIBLE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_pwd;
    }


    private void commitNewPwd(String oldPwd, String newPwd) {
        HashMap<String, String> map = new HashMap<>();
        map.put("oldpassword", oldPwd);
        map.put("newpassword", newPwd);
        Subscription subscribe = DataCenter.changePwd(map).subscribe(new HttpSubscriber<HttpResultEntity>(this, showLoading()) {
            @Override
            public void onNext(HttpResultEntity httpResultEntity) {
                if (!TextUtils.isEmpty(httpResultEntity.getMessage())) {
                    showToast(httpResultEntity.getMessage());
                    EditPwdActivity.this.finish();
                }
            }
        });
        subscription.add(subscribe);
    }

    @OnClick({R.id.tv_top})
    public void onViewClicked(View view) {
        if (TextUtils.isEmpty(etPwdOld.getText().toString())) {
            showToast("请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(etPwdNew.getText().toString())) {
            showToast("请输入新密码");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_top:
                commitNewPwd(etPwdOld.getText().toString(), etPwdNew.getText().toString());
                break;
        }
    }
}
